package net.starwifi.duobao.utils.sms;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by STX on 2015/9/29.
 */
public class SMSUtils {

    //final static String smsUrl="https://www.hfgo.com.cn/aipay_web/partnerSendSms.do";
//    final static String smsUrl="http://120.52.21.138:8080/aipay_web/partnerSendSms.do";
    final static String smsUrl="https://www.hfgo.com.cn/aiPayServPortal/html/thirdpart/partnerSendSms";

    public final static String BUY_SUCC="10002";
    public final static String SHIPPED_ING="10006";
    public final static String SHIPPED_SUCCESS="10007";
    public final static String SHIPPED_FAILED="10008";
    public final static String REFUNDED="10009";
    public final static String PRODUCT_URL = "http://dwz.cn/2cS1og";
    public static SMSResponse smsSend(SMSRequest smsRequest) {
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(smsUrl);
        System.out.println(smsRequest.toJsonStr());
        List<NameValuePair> nameValuePair=new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("sendData",smsRequest.toJsonStr()));

        HttpResponse httpResponse = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity);
            System.out.println("result="+result);
            SMSResponse smsResponse=SMSResponse.encode(result);
            System.out.println("smsResponse="+smsResponse.toString());
            return smsResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SMSResponse send(String phoneNumber, List datas,String templateId){
        SMSRequest smsRequest=new SMSRequest("100004","starwifi",phoneNumber,templateId,datas,"343D35167272373596FBA8291C6AB3B8");
        return smsSend(smsRequest);
    }

    public static SMSResponse send(String phoneNumber, String data,String templateId){
        List<String> datas=new ArrayList<String>();
        datas.add(data);
        SMSRequest smsRequest=new SMSRequest("100004","starwifi",phoneNumber,templateId,datas,"343D35167272373596FBA8291C6AB3B8");
        return smsSend(smsRequest);
    }


}
